package org.triumphxx.springboot.loopretry.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.triumphxx.springboot.loopretry.service.RetryService;

import java.time.LocalDate;
import java.util.Date;

/**
 * @author: wangyp
 * @Date: 2023/7/17
 * @Time: 10:44
 * @Desc: 测试操作实现类
 **/
@Service
@Slf4j
public class RetryServiceImpl implements RetryService {
    /**
     * 重试方法
     * @param str
     * @return
     */
    @Override
    @Retryable(value = Exception.class,maxAttempts = 3,backoff = @Backoff(delay = 2000,multiplier = 1.5),listeners = {"retryListenerBean", "retryListenerTwoBean"},interceptor = "retryInterceptor" )
    public String retry(String str) throws Exception {
        log.info("Service 请求入参为：{}",str);
        log.info("进入测试方法，目前时间为：{}",new Date());
        if ("succ".equals(str)){
            return "succ";
        }else {
            throw new Exception("异常了！");
        }


    }

    /**
     * 重试次数完成后，回调的方法
     * @param e
     * @param str
     * @return
     */
    @Override
    @Recover
    public String recover(Exception e, String str) {
        log.info("异常出现后的回调操作，入参为：{}，当前时间为：{}", str,new Date());
        return null;
    }
}
